<template>
  <div>
    <van-address-edit
      :address-info="detail"
      :area-list="areaList"
      show-delete
      show-set-default
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
    />
  </div>
</template>

<script>
import {
  reqAddressAdd,
  reqAddressList,
  reqAddressEdit
} from "../../../http/api.js";
import { areaList } from "@vant/area-data";
import { Toast } from "vant";
export default {
  data() {
    return {
      areaList,
      searchResult: [],
      detail: {}
    };
  },
  methods: {
    onSave(e) {
      if (this.$route.query.id) {
        reqAddressEdit({
          id: this.$route.query.id,
          username: e.name,
          userphone: e.tel,
          location: e.province + e.city + e.county,
          useraddress: e.addressDetail
        }).then(res => {});
      } else {
        reqAddressAdd({
          uid: JSON.parse(localStorage.getItem("user")).uid,
          username: e.name,
          userphone: e.tel,
          location: e.province + e.city + e.county,
          useraddress: e.addressDetail
        }).then(res => {
          Toast("save");
        });
      }
      this.$router.push("/site")
    }
  },
  mounted() {
    // 4.编辑进来了
    if (this.$route.query.id) {
      //此处是编辑进来的
      reqAddressList({
        uid: JSON.parse(localStorage.getItem("user")).uid,
        id: this.$route.query.id
      }).then(res => {
        let detail = res.data.list[0];

        this.detail = {
          name: detail.username,
          tel: detail.userphone,
          address: detail.location,
          areaCode: detail.regioncode,
          addressDetail: detail.useraddress,
          isDefault: detail.status == 1
        };
        this.tag = detail.label;
      });
    }
  }
};
</script>

<style>
</style>